//
//  SelectChatRoomTableViewController.m
//  LiaoTianShi
//
//  Created by lanou3g on 15/11/10.
//  Copyright © 2015年 菲克. All rights reserved.
//

#import "SelectChatRoomTableViewController.h"
#import <AVOSCloud.h>
#import <AVOSCloudIM.h>
#import "ChatModel.h"
#import "CreateChatRoomViewController.h"

#import "CHATViewController.h"


@interface SelectChatRoomTableViewController ()

@property(nonatomic,strong)NSArray * chatroomList;
@property(nonatomic,strong)NSString * roomName;
@property(nonatomic,strong)ChatModel * cr;
// 下拉刷新,自动刷新最好
@property(nonatomic,strong)UIRefreshControl * refreshController;

@end

@implementation SelectChatRoomTableViewController

-(void)loadData
{
    [ _cr printChatRoomList:^(NSArray *roomList) {
        
        _chatroomList =roomList ;
        [self.tableView reloadData];

    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _cr = [ChatModel shareChatRoomHandle];
    
    AVUser * curUser = [AVUser currentUser];
    //创建长连接
    
    [_cr connectServerWithClientID:curUser.username :^(BOOL success, NSError *error) {
        
        if (success) {
            // 请求数据
            [self loadData];
            
            NSLog(@"----------------------------------------------------------------------------------%@",_chatroomList);
        }
        else{
            NSLog(@"建立长连接失败");
        }

    }];
    
    
    
    self.refreshController = [[UIRefreshControl alloc]  init];
    [_refreshController addTarget:self action:@selector(refreshAction:) forControlEvents:(UIControlEventValueChanged)];
    _refreshController.tintColor = [UIColor blackColor];
    [self.tableView addSubview:_refreshController];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc ]initWithTitle:@"退出登录" style:UIBarButtonItemStyleDone target:self action:@selector(logOutAction:)];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc ]initWithTitle:@"创建聊天室" style:UIBarButtonItemStyleDone target:self action:@selector( creatChatRoomAction:)];
    
}

-(void)logOutAction:(UIBarButtonItem *)sendr
{
    [AVUser logOut];
    AVUser *curUser = [AVUser currentUser];
    
    NSLog(@"%@",curUser);
    [self.navigationController popViewControllerAnimated:YES];
}


// 下拉刷新
-(void)refreshAction:(id)sender
{
    [self loadData];
    [_refreshController endRefreshing];
}

// 创建聊天室
-(void)creatChatRoomAction:(UIBarButtonItem *)sender
{
    CreateChatRoomViewController * createChatRoomVC = [[CreateChatRoomViewController alloc] init];
    
    [self.navigationController pushViewController:createChatRoomVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return _chatroomList.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  
    
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:@"cell"];
        
    }
    cell.textLabel.text = [_chatroomList[indexPath.row] valueForKey:@"name"];
    
    return cell;
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 查找聊天室,进入聊天室
    // 根据 conversationID 查找聊天室连接,然后加入.
    AVObject * object = _chatroomList[indexPath.row];
    NSLog(@"%@",object.objectId);
    [_cr findChatRoomWithConversationID:object.objectId callback:^(BOOL success, NSError *error) {
        if (success) {
            NSLog(@"加入聊天室");
            CHATViewController * chatRoomVC = [[CHATViewController alloc] init];
            
            [self.navigationController pushViewController:chatRoomVC animated:YES];
        }else
        {
            NSLog(@"进入聊天室失败");
        }
    }];
    
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
